#import('dart:html');
#source('Timer.dart');

class Bar {

  num clockTick;
  Timer timer;
  int count = 0;
  int animationStepCount = 0;
  
  Bar() {
    timer = new Timer();
  }

  void runPourAnimation()
  {
    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "395px";
    leftarm.style.top = "200px";
    leftarm.src = "http://dartender.googlecode.com/svn/trunk/simple_test/leftPour1.gif";
    
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "110px";
    rightarm.style.top = "150px";
    rightarm.src = "http://dartender.googlecode.com/svn/trunk/simple_test/rightPour2.gif"; 
  }

  void runPushAnimation()
  {
    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "445px";
    leftarm.style.top = "75px";
    leftarm.src = "putaway1.gif";
    
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "65px";
    rightarm.style.top = "160px";
    rightarm.src = "righarm3.gif"; 
  }
  
  void runShakeAnimation()
  {
    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "440px";
    leftarm.style.top = "77px";
    leftarm.src = "http://dartender.googlecode.com/svn/trunk/simple_test/shakeFramesFaster.gif";
    
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "110px";
    rightarm.style.top = "150px";
    rightarm.src = "righarm3.gif"; 
  }  
  
  void run() {
    var torso = document.query("#torso");
    torso.style.position = "absolute";
    torso.style.left="300px";
    
    var bartop = document.query("#bartop");
    bartop.src = "http://dartender.googlecode.com/svn/trunk/simple_test/bartopgif.gif";;
    //document.query("#rightarm").src = "http://dartender.googlecode.com/svn/trunk/simple_test/rightPour1.gif";

    var leftarm = document.query("#leftarm");
    leftarm.style.position = "absolute";
    leftarm.style.left = "395px";
    leftarm.style.top = "200px";
    leftarm.src = "http://dartender.googlecode.com/svn/trunk/simple_test/putaway1.png";
    
    var rightarm = document.query("#rightarm");
    rightarm.style.position = "absolute";
    rightarm.style.left = "110px";
    rightarm.style.top = "150px";
    rightarm.src = "righarm3.gif"; 

    
    runPourAnimation();
  }

  void write(String message) {
    // the HTML library defines a global "document" variable
    document.query('#status').innerHTML = message;
  }
  
  void start() {
    print("starting game");
    count = 0;
    window.webkitRequestAnimationFrame(loop);
  }
  
  /**
   * this is called 60 times/sec
   */
  void loop(int time) {
    clockTick = this.timer.tick();
    this.count++;
    print("loop: count="+count+".  time="+time);
    
    document.query('#status').innerHTML = "frame count="+count.toString() + ".  animation step count="+this.animationStepCount.toString();
    
    final int numAnimationFramePerSecond= 60; // number of animation frame per second. the number of times loop() is called per second.  
    int numAnimationStepPerSecond = 3; // number of animation steps per second
    
    int numberOfFrameToSkip = (60 ~/ 3);  // skip 20 frames.  for each 20 frames, it is one animation step.  
    // or (60 / 3).toInt()
    
    if (this.count % numberOfFrameToSkip == 0) {
      this.animationStepCount++;
      print("animation step count="+this.animationStepCount);
      runAnimationStep(this.animationStepCount);
    }
    
    
    
    
    
    // stop the animation after 5 count.
    if (animationStepCount < 5) {
      window.webkitRequestAnimationFrame(loop);
    }
  }
  
  /**
   * run one animation step
   */
  void runAnimationStep(int animationStepCount) {
    print("run animation step.  animationStepCount="+animationStepCount);
    
    // content goes here.
  }
}

void main() {
  Bar bar = new Bar();
  bar.run();
  bar.start();
  
  }
